package com.qf.shop.search.listener;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import com.qf.shop.common.constants.RabbitMQCons;
import com.qf.shop.common.vo.GoodsPngVO;
import com.qf.shop.common.vo.GoodsVo;
import com.qf.shop.search.service.impl.GoodsServiceImpl;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
@Slf4j
public class EsQueueListener {

    @Autowired
    private GoodsServiceImpl goodsService;

    @RabbitListener(queues = RabbitMQCons.GOODS_TO_ESQUEUE)
    public void saveGoodsToEs(String goodsVoJSON, Channel channel, Message message) {

        try {
            // 2.插入到es中
            goodsService.addGoods(goodsVoJSON);

            // 手动ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
